package org.juggers.client;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.juggers.client.JuggersConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * ReCaptcha component
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
public class ReCaptcha
    extends SimplePanel
{

    private final JuggersConstants constants = GWT.create( JuggersConstants.class );

    /**
     * Constructor
     */
    public ReCaptcha()
    {
        super();
        getElement().setId( DOM.createUniqueId() );
    }

    @Override
    protected void onLoad()
    {
        super.onLoad();
        create( constants.captchaPublicKey(), getElement().getId(), constants.captchaTheme(), constants.captchaLang() );
    }

    @Override
    protected void onUnload()
    {
        super.onUnload();
        destroy();
    }

    /**
     * Create the reCaptcha component
     * 
     * @param publicKey API public key
     * @param elementId Element Id
     * @param theme theme
     * @param lang language
     */
    public native void create( String publicKey, String elementId, String theme, String lang ) /*-{
        $wnd.Recaptcha.create(publicKey, elementId, { theme: theme, lang: lang });
    }-*/;

    /**
     * Destroy the reCaptcha component
     */
    public native void destroy() /*-{
        $wnd.Recaptcha.destroy();
    }-*/;

    /**
     * Reload the reCaptcha component
     */
    public native void reload() /*-{
        $wnd.Recaptcha.reload();
    }-*/;

    /**
     * Get the reCaptcha challenge
     * 
     * @return reCaptcha challenge
     */
    public native String getChallenge() /*-{
        return $wnd.Recaptcha.get_challenge();
    }-*/;

    /**
     * Get the reCaptcha response
     * 
     * @return reCaptcha response
     */
    public native String getResponse() /*-{
        return $wnd.Recaptcha.get_response();
    }-*/;
}
